//
//  NSArray+AviodCrash.m
//  LzlPictureBrower
//
//  Created by luozhilin on 2018/6/5.
//  Copyright © 2018年 luozhilin. All rights reserved.
//

#import "NSArray+AviodCrash.h"
#import "YYAvoidCrash.h"

@implementation NSArray (AviodCrash)

+ (void)avoidCrashExchangeMethod {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //=================
        //   class method
        //=================
        
        //instance array method exchange
        [YYAvoidCrash exchangeClassMethod:[self class] originalMethod:@selector(arrayWithObjects:count:) newMethod:@selector(yy_AvoidCrashArrayWithObjects:count:)];

        //   instance method  族类
        Class __NSArray = NSClassFromString(@"NSArray");
        Class __NSArrayI = NSClassFromString(@"__NSArrayI");
        Class __NSSingleObjectArrayI = NSClassFromString(@"__NSSingleObjectArrayI");
        Class __NSArray0 = NSClassFromString(@"__NSArray0");
        
        
        //objectsAtIndexes:
        [YYAvoidCrash exchangeInstanceMethod:__NSArray originalMethod:@selector(objectsAtIndexes:) newMethod:@selector(yy_avoidCrashObjectsAtIndexes:)];
        
        
        //objectAtIndex:
        [YYAvoidCrash exchangeInstanceMethod:__NSArrayI originalMethod:@selector(objectAtIndex:) newMethod:@selector(yy__NSArrayIAvoidCrashObjectAtIndex:)];
        
        [YYAvoidCrash exchangeInstanceMethod:__NSSingleObjectArrayI originalMethod:@selector(objectAtIndex:) newMethod:@selector(yy__NSSingleObjectArrayIAvoidCrashObjectAtIndex:)];
        
        [YYAvoidCrash exchangeInstanceMethod:__NSArray0 originalMethod:@selector(objectAtIndex:) newMethod:@selector(yy__NSArray0AvoidCrashObjectAtIndex:)];
        
        //objectAtIndexedSubscript:
        if (AvoidCrashVersion(11.0)) {
            [YYAvoidCrash exchangeInstanceMethod:__NSArrayI originalMethod:@selector(objectAtIndexedSubscript:) newMethod:@selector(yy__NSArrayIAvoidCrashObjectAtIndexedSubscript:)];
        }
        
        
        //getObjects:range:
        [YYAvoidCrash exchangeInstanceMethod:__NSArray originalMethod:@selector(getObjects:range:) newMethod:@selector(yy_NSArrayAvoidCrashGetObjects:range:)];
        
        [YYAvoidCrash exchangeInstanceMethod:__NSSingleObjectArrayI originalMethod:@selector(getObjects:range:) newMethod:@selector(yy__NSSingleObjectArrayIAvoidCrashGetObjects:range:)];
        
        [YYAvoidCrash exchangeInstanceMethod:__NSArrayI originalMethod:@selector(getObjects:range:) newMethod:@selector(yy__NSArrayIAvoidCrashGetObjects:range:)];
    });
    
}

+ (instancetype)yy_AvoidCrashArrayWithObjects:(const id  _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt {
    
    id instance = nil;
    
    @try {
        instance = [self yy_AvoidCrashArrayWithObjects:objects count:cnt];
    }
    @catch (NSException *exception) {
        
        NSString *defaultToDo = @"AvoidCrash default is to remove nil object and instance a array.";
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
        
        //以下是对错误数据的处理，把为nil的数据去掉,然后初始化数组
        NSInteger newObjsIndex = 0;
        id  _Nonnull __unsafe_unretained newObjects[cnt];
        
        for (int i = 0; i < cnt; i++) {
            if (objects[i] != nil) {
                newObjects[newObjsIndex] = objects[i];
                newObjsIndex++;
            }
        }
        instance = [self yy_AvoidCrashArrayWithObjects:newObjects count:newObjsIndex];
    }
    @finally {
        return instance;
    }
}



//=================================================================
//                     objectAtIndexedSubscript:
//=================================================================
#pragma mark - objectAtIndexedSubscript:
- (id)yy__NSArrayIAvoidCrashObjectAtIndexedSubscript:(NSUInteger)idx {
    id object = nil;
    
    @try {
        object = [self yy__NSArrayIAvoidCrashObjectAtIndexedSubscript:idx];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = AvoidCrashDefaultReturnNil;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
    }
    @finally {
        return object;
    }
    
}


//=================================================================
//                       objectsAtIndexes:
//=================================================================
#pragma mark - objectsAtIndexes:

- (NSArray *)yy_avoidCrashObjectsAtIndexes:(NSIndexSet *)indexes {
    
    NSArray *returnArray = nil;
    @try {
        returnArray = [self yy_avoidCrashObjectsAtIndexes:indexes];
    } @catch (NSException *exception) {
        NSString *defaultToDo = AvoidCrashDefaultReturnNil;
       [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
        
    } @finally {
        return returnArray;
    }
}


//=================================================================
//                         objectAtIndex:
//=================================================================
#pragma mark - objectAtIndex:

//__NSArrayI  objectAtIndex:
- (id)yy__NSArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
    id object = nil;
    
    @try {
        object = [self yy__NSArrayIAvoidCrashObjectAtIndex:index];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = AvoidCrashDefaultReturnNil;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
    }
    @finally {
        return object;
    }
}



//__NSSingleObjectArrayI objectAtIndex:
- (id)yy__NSSingleObjectArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
    id object = nil;
    
    @try {
        object = [self yy__NSSingleObjectArrayIAvoidCrashObjectAtIndex:index];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = AvoidCrashDefaultReturnNil;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
    }
    @finally {
        return object;
    }
}

//__NSArray0 objectAtIndex:
- (id)yy__NSArray0AvoidCrashObjectAtIndex:(NSUInteger)index {
    id object = nil;
    
    @try {
        object = [self yy__NSArray0AvoidCrashObjectAtIndex:index];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = AvoidCrashDefaultReturnNil;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
    }
    @finally {
        return object;
    }
}


//=================================================================
//                           getObjects:range:
//=================================================================
#pragma mark - getObjects:range:

//NSArray getObjects:range:
- (void)yy_NSArrayAvoidCrashGetObjects:(__unsafe_unretained id  _Nonnull *)objects range:(NSRange)range {
    
    @try {
        [self yy_NSArrayAvoidCrashGetObjects:objects range:range];
    } @catch (NSException *exception) {
        
        NSString *defaultToDo = AvoidCrashDefaultIgnore;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
    } @finally {
        
    }
}


//__NSSingleObjectArrayI  getObjects:range:
- (void)yy__NSSingleObjectArrayIAvoidCrashGetObjects:(__unsafe_unretained id  _Nonnull *)objects range:(NSRange)range {
    
    @try {
        [self yy__NSSingleObjectArrayIAvoidCrashGetObjects:objects range:range];
    } @catch (NSException *exception) {
        
        NSString *defaultToDo = AvoidCrashDefaultIgnore;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
        
    } @finally {
        
    }
}


//__NSArrayI  getObjects:range:
- (void)yy__NSArrayIAvoidCrashGetObjects:(__unsafe_unretained id  _Nonnull *)objects range:(NSRange)range {
    
    @try {
        [self yy__NSArrayIAvoidCrashGetObjects:objects range:range];
    } @catch (NSException *exception) {
        
        NSString *defaultToDo = AvoidCrashDefaultIgnore;
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:defaultToDo];
        
    } @finally {
        
    }
}



@end
